package com.caiyouxi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.util.Log;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import org.nutz.http.*;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Jianghao on 2017/3/14.
 *
 * @author Howe(howechiang@gmail.com)
 */
public class HttpUtil {

    /**
     * 普通Get请求
     *
     * @param url
     * @return
     */
    public static String get(String url) {

        if (Strings.isBlank(url))
            return null;
        else if (!Strings.isUrl(url))
            return null;
        else
            return Http.get(url).getContent();
    }

    /**
     * 普通Post请求
     *
     * @param url
     * @param parms
     * @return
     */
    public static String post(String url, Map<String, Object> parms) {

        if (Strings.isBlank(url))
            return null;
        else if (!Strings.isUrl(url))
            return null;
        else if (parms.isEmpty())
            return null;
        else
            return Http.post(url, parms, 10000);
    }

    /**
     * 带Header信息的Post请求
     *
     * @param url
     * @param header
     * @param parms
     * @return
     */
    public static String post(String url, Header header, Map<String, Object> parms) {

        if (Strings.isBlank(url))
            return null;
        else if (!Strings.isUrl(url))
            return null;
        else if (Lang.isEmpty(header))
            return null;
        else if (header.getAll().isEmpty())
            return null;
        else
            return Http.post3(url, parms, header, 10000).getContent();
    }

    /**
     * 带Header信息Get请求
     *
     * @param url
     * @param header
     * @return
     */
    public static String get(String url, Header header) {

        if (Strings.isBlank(url))
            return null;
        else if (!Strings.isUrl(url))
            return null;
        else if (Lang.isEmpty(header))
            return null;
        else if (header.getAll().isEmpty())
            return null;
        else
            return Http.get(url, header, 10000).getContent();
    }

    /**
     * Post发送Json请求
     *
     * @param url
     * @param json
     * @return
     */
    public static String post(String url, String json) {

        if (Strings.isBlank(url))
            return null;
        else if (!Strings.isUrl(url))
            return null;
        else if (Lang.isEmpty(json))
            return null;
        else {
            Request req = Request.create(url, Request.METHOD.POST);
            req.getHeader().set("Content-Type", "application/json;charset=UTF-8");
            req.setData(json.toString());
            Response resp = Sender.create(req).setTimeout(6000).send();
            return resp.getContent();
        }

    }

    public static final MediaType JSONTYPE = MediaType.parse("application/json; charset=utf-8");
    public static ResData post_body_json (String url, Object obj) {
        Log.d("==========send_url======:"+url,"obj:"+obj.toString());
        OkHttpClient okHttpClient = new OkHttpClient();
        ResData resData =null;
        //创建一个RequestBody(参数1：数据类型 参数2传递的json串)
        RequestBody requestBody = RequestBody.create(JSONTYPE, JSONObject.toJSON(obj).toString());
        //创建一个请求对象
        okhttp3.Request request = new okhttp3.Request.Builder().url(url).post(requestBody).build();
        //发送请求获取响应
        try {
            okhttp3.Response response=okHttpClient.newCall(request).execute();
            String response_body = response.body().string();
            Log.d("==========return_str======:"+response_body);
            //请在各自业务中处理相关业务逻辑，此处返回的与service返回的一样
            resData =  JSON.parseObject(response_body,ResData.class);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("请求异常" + e.getMessage());
        }

        return resData;

    }
}
